﻿#region (C) Header Region @ 2012
//====================================================
// 文件: MPCalendar.cs
// 描述: 
// 路径: Antu.WebControls\Calendar\MPCalendar.cs
// ******************版本1 ******************
// 人员: 蔡金保    时间 : 2012/11/15 11:26:39
// 注释:
//====================================================
#endregion

using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System;
using System.Reflection;
using System.IO;

namespace Antu.WebControls
{
    /// <summary>
    ///日期选择控件,支持是否只读，可编辑，以及格式分隔符（以后可以慢慢扩展） cjb
    /// </summary>
    [DefaultProperty("Text"), ToolboxData("<{0}:MPCalendar runat=server></{0}:MPCalendar>")]
    public class MPCalendar : WebControl, IPostBackDataHandler
    {
        [Bindable(true), Category(""), DefaultValue("")]
        public string Text
        {
            get
            {
                if (ViewState["Text"] != null)
                {
                    return ViewState["Text"].ToString();
                }
                else
                {
                    return "";
                }
            }

            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    ViewState["Text"] = Convert.ToDateTime(value).ToString("yyyy" + FormatSplit + "MM" + FormatSplit + "dd");
                }
                else
                {
                    ViewState["Text"] = value;
                }
            }
        }

        [Bindable(true), Category(""), DefaultValue(false)]
        public bool ReadOnly
        {
            get
            {
                if (ViewState["ReadOnly"] != null)
                {
                    return (bool)ViewState["ReadOnly"];
                }
                else
                {
                    return false;
                }
            }

            set
            {
                ViewState["ReadOnly"] = value;
            }
        }
        [Bindable(true), Category(""), DefaultValue(true)]
        public new bool Enabled
        {
            get
            {
                if (ViewState["Enabled"] != null)
                {
                    return (bool)ViewState["Enabled"];
                }
                else
                {
                    return true;
                }
            }

            set
            {
                ViewState["Enabled"] = value;
            }
        }

        [Bindable(true), Category(""), DefaultValue(true)]
        public new string Style
        {
            get
            {
                return (string)ViewState["Style"];
            }
            set
            {
                ViewState["Style"] = value;
            }
        }
        /// <summary>
        /// 目前只支持单格式分隔符 如 FormatSplit="-"
        /// </summary>
        [Bindable(true), Category(""), DefaultValue("-")]
        public string FormatSplit
        {
            get
            {
                if (ViewState["FormatSplit"] != null)
                {
                    return ViewState["FormatSplit"].ToString();
                }
                else
                {
                    return "-";
                }
            }
            set
            {
                ViewState["FormatSplit"] = value;
            }
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (!Page.ClientScript.IsStartupScriptRegistered(GetType(), "CalendarScript"))
            {
                this.Page.ClientScript.RegisterClientScriptResource(GetType(), "Antu.WebControls.Calendar.Calendar.js");
            }
        }
        protected override void Render(HtmlTextWriter output)
        {
            output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
            output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
            if (!string.IsNullOrEmpty(this.CssClass)) { output.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass); }
            if (this.Text != "" && this.Text != null) { output.AddAttribute(HtmlTextWriterAttribute.Value, this.Text); }
            if (string.IsNullOrEmpty(this.Style))
            {
                output.AddAttribute(HtmlTextWriterAttribute.Style, "cursor:hand");
            }
            else
            {
                this.Style = "cursor:hand;" + this.Style;
                output.AddAttribute(HtmlTextWriterAttribute.Style, this.Style);
            }
            output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
            output.AddAttribute(HtmlTextWriterAttribute.Title, "点击选择日期");
            output.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "true");
            if (!ReadOnly)
            {
                if (!string.IsNullOrEmpty(FormatSplit))
                {
                    output.AddAttribute(HtmlTextWriterAttribute.Onclick, "javascript:setformat('" + FormatSplit + "');setday(this);");
                }
                else
                {
                    output.AddAttribute(HtmlTextWriterAttribute.Onclick, "javascript:setday(this);");
                }
            }
            if (!Enabled)
            {
                output.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
            }
            output.RenderBeginTag(HtmlTextWriterTag.Input);
            output.RenderEndTag();


        }

        /// <summary>
        /// 处理回发数据。
        /// </summary>
        /// <param name="postDataKey"></param>
        /// <param name="postCollection"></param>
        /// <returns></returns>
        public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            String presentValue = Text;
            String postedValue = postCollection[postDataKey];
            if (presentValue == null || !presentValue.Equals(postedValue))
            {
                Text = postedValue;
                return true;
            }
            return false;
        }
        public virtual void RaisePostDataChangedEvent()
        {
            OnTextChanged(EventArgs.Empty);
        }
        public event EventHandler TextChanged;
        protected virtual void OnTextChanged(EventArgs e)
        {
            if (TextChanged != null)
                TextChanged(this, e);
        }
    }
}



